/**
 * Created by limeng on 16/6/12.
 */
delegateApp.controller("RoleManageCtrl", function ($scope, $http) {
    $scope.roleArray = [];

    $scope.refreshRoleList = function () {
        $http.get("/role/").success(function (data) {
            $scope.roleArray = data;
            $scope.newRoleName = "";
            $scope.edittingRole = {};
        }).error(function () {
            bootbox.alert("获取角色信息失败!");
        });
    };
    $scope.refreshRoleList();

    $scope.deleteRole = function (role) {
        bootbox.confirm("真的要删除这个角色吗?删除后,所有该角色的用户将自动归入“教师代表”角色下!",
            function (res) {
            if (res) {
                $http.delete("/role/"+role.id).success(function (data) {
                    if (data.state) {
                        bootbox.alert("删除成功!");
                        $scope.refreshRoleList();
                    } else {
                        bootbox.alert("删除失败,原因是:"+data.result);
                    }
                })
            }
        });
    };

    $scope.newRole = function () {
        $("#newRoleDialog").modal();
    };
    $scope.doNewRole = function () {
        if ($scope.newRoleName == '') {
            bootbox.alert("角色名不允许为空!");
            return;
        }
        $http.post("/role/", {
            name : $scope.newRoleName
        }).success(function (data) {
            if (data.state) {
                bootbox.alert("成功新建了角色!");
                $scope.refreshRoleList();
            } else {
                bootbox.alert("创建角色失败!原因是:" + data.result);
            }
        })
    };

    $scope.editRole = function (role) {
        $("#editRoleDialog").modal();
        $scope.edittingRole = role;
        $scope.editRoleName = role.name;
    };
    $scope.changeRoleName = function () {
        $http.put("/role/name", {
            id : $scope.edittingRole.id,
            name : $scope.editRoleName
        }).success(function (data) {
            if (data.state) {
                bootbox.alert("修改成功!");
                $scope.edittingRole.name = $scope.editRoleName;
            } else {
                bootbox.alert("修改失败!原因为:"+data.result);
            }
        })
    };

    $scope.changePermission = function (role) {
        $scope.onCheckingRole = role;
        var setting = {
            view: {
                selectedMulti: false
            },
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            }
        };
        var generateCategory = function(permissions) {
            var category = $scope.permissionCategory;
            permissions.sort(function (a, b) {
                var numA = parseInt(a.category);
                var nubB = parseInt(b.category);

                if(numA > nubB) {
                    return 1;
                } else {
                    return -1;
                }
            });
            for (var i = 0; i < permissions.length; i++) {
                var permission = permissions[i];
                var element = {
                    id : permission.category,
                    name : permission.description,
                    trueId : permission.id
                };
                if (permission.category.length != 2) {
                    element.pId = permission.category.slice(0, 2);
                }
                category.push(element);
            }
        };
        $scope.permissionCategory = [];
        $http.get('/permission').success(function (data) {
            generateCategory(data);
            var zNodes = $scope.permissionCategory;
            var checkedPermissions = role.permissions;
            for (var i = 0; i < checkedPermissions.length; i++) {
                for (var j = 0; j < zNodes.length; j++) {
                    if (checkedPermissions[i].category == zNodes[j].id) {
                        zNodes[j].checked = true;
                    }
                }
            }
            $.fn.zTree.init($("#permissionTree"), setting, zNodes);
            $('#setupPermissionDialog').modal();
        }).error(function () {
            bootbox.alert("获取权限信息失败!");
        });
    };

    $scope.doChangePermission = function () {
        var role = $scope.onCheckingRole;
        var checkedNode = $.fn.zTree.getZTreeObj("permissionTree").getCheckedNodes(true);

        var postList = [];
        for (var i = 0; i < checkedNode.length; i++) {
            postList.push(checkedNode[i].trueId);
        }

        $http.post("/role/changePermissions/"+role.id, postList).success(function (data) {
            if (data.state) {
                bootbox.alert("修改成功!");
                $scope.refreshRoleList();
            } else {
                bootbox.alert("修改失败!原因是:"+data.result);
            }
        }).error(function () {
            bootbox.alert("服务器没有响应!");
        })

    };

    $scope.addUser = function(role) {
        $scope.onAddUserRole = role;
        $scope.delegateList = [];
        $http.get("/delegate/").success(function (data) {
            for (var i = 0; i < data.length; i++) {
                if (data[i].role.id != $scope.onAddUserRole.id) {
                    $scope.delegateList.push(data[i]);
                }
            }
            $("#addUserDialog").modal();
        }).error(function () {
            bootbox.alert("获取教师代表列表失败!");
        })
    };

    $scope.doAddUser = function () {
        var userIdList = [];
        var cb_addUser = document.getElementsByName("cb_addUser");
        if (cb_addUser.length > 0) {
            for (var i = 0; i < cb_addUser.length; i++) {
                if (cb_addUser[i].checked) {
                    userIdList.push(cb_addUser[i].value);
                }
            }
        }
        $http.post("/role/addUserTo/"+$scope.onAddUserRole.id,
            userIdList).success(function (data) {
            if (data.state) {
                bootbox.alert("成功修改了"+data.successCount+"位用户的角色信息!");
            } else {
                bootbox.alert("修改失败!原因是:"+data.result);
            }
        });
    }
});